// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	/**
	 * 错题库分页列表
	 * @param {number} pageNo 页码
	 * @param {number} pageSize 每页条数
	 * @returns {object} 返回值描述
	 */
	async topic_problem_list(pageNo, pageSize) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate
		return await dbJQL.collection('topic-problems').aggregate()
			.match({
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid')
			})
			.lookup({
				from: 'questions-topic',
				let: {
					topic_id: "$topic_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(['$_id', '$$topic_id'])))
					.done(),
				as: 'topic'
			})
			.project({
				topic: $.arrayElemAt(['$topic', 0])
			})
			.sort("create_date", "desc")
			.skip((pageNo - 1) * pageSize).limit(pageSize).end()
	},
	/* 获取错题库所有题目 */
	async topic_problem_all() {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		const dbCmd = dbJQL.command
		const $ = dbCmd.aggregate
		return await dbJQL.collection('topic-problems').aggregate()
			.match({
				user_id: dbJQL.getCloudEnv('$cloudEnv_uid')
			})
			.lookup({
				from: 'questions-topic',
				let: {
					topic_id: "$topic_id"
				},
				pipeline: $.pipeline().match(dbCmd.expr($.eq(['$_id', '$$topic_id'])))
					.lookup({
						from: 'topic-options',
						let: {
							topic_id: '$_id'
						},
						pipeline: $.pipeline().match(
							dbCmd.expr($.eq(['$topic_id', '$$topic_id']))
						).done(),
						as: 'options'
					})
					.lookup({
						from: 'opendb-note',
						let: {
							topic_id: '$_id'
						},
						pipeline: $.pipeline().match(
							dbCmd.expr($.and([
								$.eq(['$topic_id', '$$topic_id']),
								$.eq(['$flag', false]),
								$.eq(['$user_id', dbJQL.getCloudEnv('$cloudEnv_uid')])
							]))
						).done(),
						as: 'note'
					})
					.project({
						analysis: 1,
						answer: 1,
						answer_content: 1,
						difficulty: 1,
						note: $.arrayElemAt(['$note', 0]),
						options: 1,
						question_content: 1,
						question_storehouse_id: 1,
						question_type: 1,
						user_answer: 1
					})
					.done(),
				as: 'topic'
			})
			.project({
				topic: $.arrayElemAt(['$topic', 0])
			})
			.sort("create_date", "desc").end()
	},
	/**
	 * 错题库所有数据
	 * @param {Array} data 所有错题的id
	 * @returns {object} 返回值描述
	 */
	async topic_problem_add(arrs = []) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		let res1 = await dbJQL.collection('topic-problems').where({
			topic_id: dbJQL.command.in(arrs),
			user_id: dbJQL.getCloudEnv('$cloudEnv_uid')
		}).get()
		if (res1.errCode != 0) {
			return res1
		}
		res1.data.forEach(item => {
			arrs.splice(item.topic_id, 1)
		})
		//
		let data = arrs.map(item => {
			return {
				topic_id: item
			}
		})
		if (data.length == 0) {
			return res1
		}
		return await dbJQL.collection('topic-problems').add(data)
	},
	/* 删除错题库数据 */
	async topic_problem_delete(topic_id) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		return await dbJQL.collection('topic-problems').where({
			topic_id
		}).remove()
	}
}